﻿namespace LiteBlog.Components
{
    using LiteBlog.Common.Entities;
    using System.Web;
    using LiteBlog.Common.Enums;
    using LiteBlog.Components.Enums;
    using LiteBlog.Components.Contracts;
    using LiteBlog.Repositories.Contracts;
    using WURFL.Config;
    using WURFL;

    public class SettingsComp : BaseComp<ISettingsRepository>, ISettingsComp
    {
        public SettingsComp(ISettingsRepository data, ICacheComp cache) : base(data, cache) { }

        public Settings Load()
        {
            var settings = _cache.Get<Settings>(CacheType.Settings);
            if (settings == null)
            {
                settings = _data.Load();
                _cache.Put(CacheType.Settings, settings);
            }
            if (!string.IsNullOrEmpty(settings.ImageUrl))
                settings.ImageUrl = VirtualPathUtility.ToAbsolute(settings.ImageUrl);
            return settings;
        }

        public MethodResult Save(Settings settings)
        {
            if (!string.IsNullOrEmpty(settings.ImageUrl))
                settings.ImageUrl = VirtualPathUtility.ToAppRelative(settings.ImageUrl);
            _data.Save(settings);
            return MethodResult.SaveSettingsSuccess;
        }

        public MethodResult Update(SettingType settingType, object value)
        {
            var settings = Load();
            switch (settingType)
            {
                case SettingType.Name:
                    settings.Name = (string)value;
                    break;
                case SettingType.Tagline:
                    settings.Tagline = (string)value;
                    break;
                case SettingType.UseImage:
                    settings.UseImage = (bool)value;
                    break;
                case SettingType.ImageUrl:
                    settings.ImageUrl = (string)value;
                    settings.ImageUrl = VirtualPathUtility.ToAppRelative(settings.ImageUrl);
                    break;
                case SettingType.GATrackingCode:
                    settings.GATrackingCode = (string)value;
                    break;
                case SettingType.PostCount:
                    settings.PostCount = (int)value;
                    break;
                case SettingType.Skin:
                    settings.Skin = (string)value;
                    break;
                case SettingType.Timezone:
                    settings.Timezone = (string)value;
                    break;
                case SettingType.CommentModeration:
                    settings.CommentModeration = (bool)value;
                    break;
                case SettingType.LogFile:
                    settings.LogFile = (string)value;
                    break;
                case SettingType.LogLevel:
                    settings.LogLevel = (string)value;
                    break;
                case SettingType.AccessToken:
                    settings.AccessToken = (string)value;
                    break;
            }
            return Save(settings);
        }

        private static readonly string WurflDataFilePath = "~/App_Data/wurfl-latest.zip";
        public IWURFLManager LoadWurflManager()
        {
            var wurflDataFile = HttpContext.Current.Server.MapPath(WurflDataFilePath);
            var configurer = new InMemoryConfigurer()
                .MainFile(wurflDataFile);
            var manager = WURFLManagerBuilder.Build(configurer);
            _cache.Put(CacheType.Wurfl, manager);
            return manager;
        }

        public IWURFLManager GetWurflManager()
        {
            var manager = _cache.Get<IWURFLManager>(CacheType.Wurfl);
            if (manager == null)
                manager = LoadWurflManager();
            return manager;
        }
    }
}